function residual = func_moments_altbenefit(x,P)

sigma = x(1);
sigma_star = x(2);
A = x(3);
Astar = x(4);

func_b = @(b) (P.eta-1)*log(b)+2*(P.eta-1)-P.eta_f*(P.eta-1)-P.lambda_f*b^P.lambda;
[b_me] = fzero(func_b,0.41);

krw_star_me = ((P.alpha*(1-sigma_star)*Astar*((1/P.beta+P.delta_rw-1)/(P.alpha*sigma_star*Astar))^((P.alpha*(1-sigma_star)-1) /(P.alpha*(1-sigma_star))))/(1/P.beta+P.delta_us-1))^((P.alpha*(1-sigma_star))/(1-P.alpha));
kus_star_me = ((1/P.beta+P.delta_rw-1)/(P.alpha*sigma_star*Astar))^(1/(P.alpha*(1-sigma_star)))*krw_star_me^((1-sigma_star*P.alpha)/(P.alpha*(1-sigma_star)));
    
krw_me = ((P.alpha*sigma*A*((1/P.beta+P.delta_us-1)/(P.alpha*(1-sigma)*A))^((P.alpha*sigma-1)/(P.alpha*sigma)))/(1/P.beta+P.delta_rw-1))^((P.alpha*sigma)/(1-P.alpha));
kus_me = ((1/P.beta+P.delta_us-1)/(P.alpha*(1-sigma)*A))^(1/(P.alpha*sigma))*krw_me^((1-(1-sigma)*P.alpha)/(P.alpha*sigma));
    
residual(1) = kus_me/(kus_me+kus_star_me) - P.home_bias;
residual(2) = -b_me + kus_star_me - krw_me - P.NFA_us;
residual(3) = A*(krw_me)^(P.alpha*(1-sigma))*(kus_me)^(P.alpha*sigma) - 1;
residual(4) = Astar*(krw_star_me)^(P.alpha*sigma_star)*(kus_star_me)^(P.alpha*(1-sigma_star)) - P.gdp_ratio;